﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Threading;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;

namespace Gao.Poker.App.ViewModel
{
    public class PlayGameViewModel : BaseViewModel
    {
        private ObservableCollection<BonusCalculation> _bonusCalculations;
        private Competition _competition;
        private readonly DispatcherTimer timer;
        private int _currentLevel;
        private int _totalGamePerson;
        private int _gameAverageStack;
        private Int32 _competitionID;

        public Int32 CompetitionID
        {
            get { return _competitionID; }
            set
            {
                _competitionID = value;
                OnPropertyChanged("Competition");
            }
        }

        public int CurrentLevel
        {
            get { return _currentLevel; }
            set
            {
                _currentLevel = value;
                OnPropertyChanged("TotalBet");
                OnPropertyChanged("BonusCalculations");
                OnPropertyChanged("Competition");
                OnPropertyChanged("NextCompetitionLevel");
                OnPropertyChanged("TotalGamePerson");
                OnPropertyChanged("GameAverageStack");
            }
        }

        public Competition Competition
        {
            get
            {
                if (_competitionID > 0)
                _competition = CompetitionService.Instance().GetCompetition(_competitionID);
                OnPropertyChanged("TotalBet");
                OnPropertyChanged("BonusCalculations");
                OnPropertyChanged("NextCompetitionLevel");
                OnPropertyChanged("TotalGamePerson");
                OnPropertyChanged("GameAverageStack");
                return _competition;
            }
            set {
                _competition = value;
            }
        }

        public Decimal TotalBet
        {
            get
            {
                if (_competition == null)
                {
                    return 0;
                }

                return _competition.TotalPoints;
            }
        }

        public PlayGameViewModel()
        {
            _bonusCalculations = new ObservableCollection<BonusCalculation>();
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 10);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            OnPropertyChanged("Competition");
            //OnPropertyChanged("TotalBet");
            //OnPropertyChanged("BonusCalculations");
            //OnPropertyChanged("NextCompetitionLevel");
            //OnPropertyChanged("TotalGamePerson");
            //OnPropertyChanged("GameAverageStack");
        }


        /// <summary>
        /// 当前级别
        /// </summary>
        public CompetitionLevel CompetitionLevel
        {
            get
            {
                return _competition.CompetitionLevels.FirstOrDefault(cl => cl.Level == CurrentLevel);
            }
        }

        /// <summary>
        /// 下一级别
        /// </summary>
        public CompetitionLevel NextCompetitionLevel
        {
            get
            {
                if (_competition == null) return null;
                CompetitionLevel level = null;
                for (int i = 0; i < _competition.CompetitionLevels.Count; i++)
                {
                    var cl = _competition.CompetitionLevels[i];
                    if (CurrentLevel + 1 == cl.Level)
                    {
                        level = cl;
                    }
                }
                return level;
            }
        }

        public int TotalGamePerson
        {
            get
            {
                if (_competition != null)
                {
                    _totalGamePerson = CompetitionEntryService.Instance().GetGamePerson(_competition.ID).Count;
                }
                return _totalGamePerson;
            }
            set { _totalGamePerson = value; }
        }

        public int GameAverageStack
        {
            get
            {
                if (TotalGamePerson > 0)
                    _gameAverageStack = (int) (TotalBet/TotalGamePerson);
                return _gameAverageStack;
            }
            set { _gameAverageStack = value; }
        }

        public ObservableCollection<BonusCalculation> BonusCalculations
        {
            get
            {
                if (_competition == null) return null;
                var bounse = new BounsService().GetBounses(_competition.ID);
                _bonusCalculations.Clear();
                foreach (var bouns in bounse)
                {
                    _bonusCalculations.Add(new BonusCalculation(bouns, _competition));
                }

                return _bonusCalculations;
            }
            set { _bonusCalculations = value; }
        }
    }
}
